package github.leavesczy.reactivehttpsamples.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;

import github.leavesczy.reactivehttpsamples.R;
import kotlin.reflect.jvm.internal.impl.util.Check;

public class BridgeCheckBoxItem extends LinearLayout {
    private CheckBox checkBox;
    public BridgeCheckBoxItem(Context context) {
        this(context, null);
    }

    public BridgeCheckBoxItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BridgeCheckBoxItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        try (TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BridgeCheckBoxItem)) {
            String leftText = ta.getString(R.styleable.BridgeCheckBoxItem_bcbLeftText);
            int leftTextColor = ta.getColor(R.styleable.BridgeCheckBoxItem_bcbLeftTextColor, Color.BLACK);
            float leftTextSize = ta.getDimension(R.styleable.BridgeCheckBoxItem_bcbLeftTextSize, 14);

            String rightCheckText = ta.getString(R.styleable.BridgeCheckBoxItem_android_text);

            setOrientation(LinearLayout.HORIZONTAL);
            setGravity(Gravity.CENTER_VERTICAL);

            setWeightSum(5);

            //添加左侧 Text
            TextView leftTextView=new TextView(context);
            leftTextView.setText(leftText);
            leftTextView.setTextColor(leftTextColor);
            leftTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,leftTextSize);
            LayoutParams leftParams=new LayoutParams(0,LayoutParams.MATCH_PARENT);
            leftParams.weight=2;
            leftParams.leftMargin= SizeUtils.dp2px(10);
            leftTextView.setGravity(Gravity.CENTER_VERTICAL);
            addView(leftTextView,leftParams);

            //添加右侧的 CheckBot
            checkBox=new CheckBox(context);
            if(StringUtils.isEmpty(rightCheckText)){
                checkBox.setText("有");
            }else {
                checkBox.setText(rightCheckText);
            }
            LayoutParams rightParams=new LayoutParams(0,LayoutParams.MATCH_PARENT);
            rightParams.weight=3;
            rightParams.rightMargin= SizeUtils.dp2px(10);
            checkBox.setGravity(Gravity.CENTER_VERTICAL);
            addView(checkBox,rightParams);


            setBackgroundResource(R.drawable.shape_bridge_edit_item_bg);

        }
    }


    public void setCheckBoxEnable(boolean enabled){
        checkBox.setEnabled(enabled);
    }

    public boolean getCheckBoxChecked(){
        return checkBox.isChecked();
    }

    public void setCheckBoxChecked(boolean checked){
        checkBox.setChecked(checked);
    }
}
